﻿#region License

// PentaBiz - Sustainable Software Development Framework Copyright (C) 2013 Zoltán Csizmazia
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using PentaBiz.Cloud.Services;
using PentaBiz.Cloud.Services.MetaService;


namespace PentaBiz.Studio.Plugins.MetaServer.Services
{
    public class MetaCommandPluginService : PluginService, INotifyPropertyChanged
    {
        private static volatile GenericWcfServiceHost<MetaCommandService> MetaCommandService;

        public static readonly MetaCommandPluginService Instance = new MetaCommandPluginService();
        private static readonly object _metaCommandServiceLock = new object();
        public event PropertyChangedEventHandler PropertyChanged;

        internal GenericWcfServiceHost<MetaCommandService> GetMetaCommandService()
        {
            if (MetaCommandService == null)
            {
                lock (_metaCommandServiceLock)
                {
                    if (MetaCommandService == null)
                    {
                        return MetaCommandService = new GenericWcfServiceHost<MetaCommandService>();
                    }
                }
            }

            return MetaCommandService;
        }

        protected override void OnStart()
        {
            GetMetaCommandService().Start();
        }

        protected override void OnStop()
        {
            GetMetaCommandService().Stop();
        }

        protected override void OnRestart()
        {
            GetMetaCommandService().Stop();
            GetMetaCommandService().Start();
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class MetaDbQueryPluginService : PluginService, INotifyPropertyChanged
    {
        private static volatile MetaDbQueryServiceHost MetaDbQueryServiceHost;
        private static readonly object _metaDbQueryServiceHostLock = new object();
        public static readonly MetaDbQueryPluginService Instance = new MetaDbQueryPluginService();
        public event PropertyChangedEventHandler PropertyChanged;

        internal MetaDbQueryServiceHost GetMetaDbQueryServiceHost()
        {
            if (MetaDbQueryServiceHost == null)
            {
                lock (_metaDbQueryServiceHostLock)
                {
                    if (MetaDbQueryServiceHost == null)
                    {
                        return MetaDbQueryServiceHost = new MetaDbQueryServiceHost();
                    }
                }
            }

            return MetaDbQueryServiceHost;
        }

        protected override void OnStart()
        {
            GetMetaDbQueryServiceHost().Start();
        }

        protected override void OnStop()
        {
            GetMetaDbQueryServiceHost().Stop();
        }

        protected override void OnRestart()
        {
            GetMetaDbQueryServiceHost().Stop();
            GetMetaDbQueryServiceHost().Start();
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}